package com.atguigu.udmp.web.userprofile.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.udmp.web.userprofile.bean.UserEventDefine;
import com.atguigu.udmp.web.userprofile.service.UserEventDefineService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户事件定义 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2023-10-27
 */
@RestController
@RequestMapping("/userprofile/user-event-define")
@CrossOrigin
public class UserEventDefineController {


    @Autowired
    UserEventDefineService userEventDefineService;

    @GetMapping("/list")
    public String getList(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        //分页
        Integer rowNo = (pageNo - 1) * pageSize;
        QueryWrapper<UserEventDefine> queryWrapper = new QueryWrapper<UserEventDefine>()
                .select("id", "event_name", "event_source", "event_desc", "create_time", "update_time")
                .last("limit " + rowNo + "," + pageSize);
        List<UserEventDefine> eventDefineList = userEventDefineService.list(queryWrapper);
        Integer count = userEventDefineService.count();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", eventDefineList);
        jsonObject.put("total", count);
        return jsonObject.toJSONString();
    }

    @GetMapping("/detail/{id}")
    public String getDetail(@PathVariable("id") String id) {
        UserEventDefine userEventDefine = userEventDefineService.getById(id);
        return JSON.toJSONString(userEventDefine);
    }

    @PostMapping("/detail")
    public String getDetail(@RequestBody UserEventDefine userEventDefine) {

        //更新日期
        if (userEventDefine.getId() == null) {
            userEventDefine.setCreateTime(new Date());
        } else {
            userEventDefine.setUpdateTime(new Date());
        }
        userEventDefineService.saveOrUpdate(userEventDefine);
        return JSON.toJSONString(userEventDefine);
    }

    @DeleteMapping("/detail/{id}")
    public String deleteDetail(@RequestBody UserEventDefine userEventDefine) {
        userEventDefineService.removeById(userEventDefine);
        return "success";
    }


}
